<?php
class Syllabus_model extends Model {
    function Syllabus() {
        parent::Model();
    }
    /////添加课程///////
    function addCourse($addCourse_options) {
    //学年
        $coursename=$addCourse_options['coursename'];
        $teacherid=$addCourse_options['teacherid'];
        $year=$addCourse_options['year'];
        $showdata['year']=$year;
        $ii=count($coursename);
        for($i=0;$i<count($coursename);$i++) {
        //插入数据数组
            $data=array(
                'coursename'=>$coursename[$i],
                'teacherid'=>$teacherid[$i],
                'yearid'=>$year
            );
            $this->db->insert('course',$data);
        }
    }
    ///添加课表////
    function addsyllabus($add_options) {
        $query=$this->db->insert('syllabus',$add_options);
        return $query;
    }
    ///获取班级///
    function getClass() {
        $query=$this->db->query("select * from class order by name asc");
        return $query;
    }

    //按照id获取班级
    function getClass_by_id($id){
        $query=$this->db->query("select * from class where id='$id'");
        return $query;
    }

    /////搜索学期//////
    function getyear() {
        $query=$this->db->get('year');
        $result=$query->result();
        $year=array();
        foreach($result as $i) {
            $yearid=$i->id;
            $yearname=$i->year;
            $yearname=$this->replace_year($yearname);
            $year[$yearid]=$yearname;
        }
        return $year;
    }
    //按照id获取学期名
    function getyear_by_id($yearid) {
        $query=$this->db->query("select * from year where id='$yearid' order by year asc");
        return $query;
    }
    //区别学期
    function replace_year($yearname) {
        if(substr($yearname,-1,1)=='0') {
            return $yearname=substr($yearname,0,4).'年 上半学期';
        }else {
            return $yearname=substr($yearname,0,4).'年 下半学期';
        }
    }
    ////按学期搜索课程///////
    function getcourse_by_year($yearid) {
        $query=$this->db->query("select * from course where yearid='$yearid' order by coursename asc");
        return $query;
    }
    /////按课程id搜索课程////////
    function getcourse_by_id($id) {
        $query=$this->db->query("select * from course where id='$id'");
        return $query;
    }
    ////根据id直接获取课程名称////
    function getcourse_name_by_id($id) {
        if($id!=0) {
            $query=$this->db->query("select * from course where id='$id'");
            $result=$query->result();
            foreach($result as $row) {
                $result=$row->coursename;
            }
        }else {
            $result="没有课";
        }
        return $result;
    }
    ////根据班级与学期搜索课表//////
    function getsyllabus_by_class($getCourse) {
        $classid=$getCourse['classid'];
        $year=$getCourse['year'];
        $query=$this->db->query("select * from syllabus where class='$classid' and year='$year'");
        return $query;
    }
    //删除课程
    function deletecourse($courseid) {
        foreach($courseid as $row) {
            $query=$this->db->query("delete from course where id='$row'");
        }
    }
    //编辑课程
    function editcourse($cid,$cme,$tid) {
        $query=$this->db->query("update course set coursename='$cme' , teacherid='$tid' where id='$cid'");
    }

    //判断课程是否存在//
    function course_is_exist($array){
        $options = array('yearid' => $array['year'], 'coursename' => $array['course'], 'teacherid' => $array['teacher']);
        $this->db->where($options);
        $query=$this->db->get('course');
        return $query;
    }

    //查看课表是否已经存在
    function syllabus_is_exist($add_options) {
    //班级id
        $cid=$add_options['class'];
        //指定学期
        $cyr=$add_options['year'];
        $query=$this->db->query("select * from syllabus where class='$cid' and year='$cyr'");
        if($query->result()) {
            return true;
        }else {
            return false;
        }
    }
    //删除课表//
    function delete_syllabus($id) {
        $this->db->where('id', $id);
        $query=$this->db->delete('syllabus');
        return $query;
    }

    //更新课表///
    function update_syllabus($options){
        $y=$options['year'];
        $t=$options['content'];
        $i=$options['id'];
        $c=$options['classid'];
        $array = array('year' => $y, 'id' => $i, 'class' => $c);
        $this->db->where($array);
        $query=$this->db->update('syllabus',array('content'=>$t));
        return $query;
    }

    //激活学期id
    function active_period($id){
        //重置所有学期
        $t=array('current'=>'0');
        $this->db->update('year',$t);
        $this->db->where('id',$id);
        //激活学期
        $t=array('current'=>'1');
        $query=$this->db->update('year',$t);
        return $query;
    }
    //获取当前激活的学期
    function get_current_year(){
        $query=$this->db->query("select * from year where current='1'");
        return $query;
    }
}
?>
